<?php
	include_once 'system/main.php';
    include 'header.php';
	if ( !isset( $_SESSION['username'] ) ) {
		echo "Πρέπει να συνδεθείς για να μπορείς να ψηφίσεις και να δεις τα αποτελέσματα.";
	}
	else {
		if ( isset ($_POST['voted']) && !empty($_POST['voted']) ) {
			$choice = $_POST['voted'];
			$pollid = $_SESSION['pollid'];
			$userid = $_SESSION['userid'];	
			$res2 = $purevote->db_get_ifvoted("$pollid", "$userid", "userid");
			foreach ( $res2 as $poll ) {
				$voted = 1;
			}
			if ($voted == 1) {
				echo "<script> window.location = \"./vote.php\"</script>";
			}
			else {	
				$purevote->db_apply_vote("$pollid", "$choice");
				$purevote->db_set_voted("$userid", "$pollid");
				echo "<script> window.location ='./vote.php?id=".$pollid."'</script>";
			}
		}
		else {
			echo "Δεν έχεις επιλέξει κάποια απάντηση.";
		}
	}				 
?>
